package com.keyancompany.zhongbaopingtai.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.CircleThemeAdapter;
import com.keyancompany.zhongbaopingtai.adapter.MyCircleAdapter;
import com.keyancompany.zhongbaopingtai.api.CircleAPI;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.CircleModel;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.CustomEditText;
import com.keyancompany.zhongbaopingtai.weight.IListView;
import com.keyancompany.zhongbaopingtai.weight.ISwipeRefreshLayout;

import java.util.List;

import butterknife.Bind;

/**
 * 查找圈子
 */
public class SearchCircleThemeActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.filter_edit)
    CustomEditText filterEdit;
    @Bind(R.id.listView)
    IListView listView;
    @Bind(R.id.swipe_layout)
    ISwipeRefreshLayout swipeLayout;

    MyCircleAdapter mAdapter;
    private String keyWords;

    private int currentPage, total;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_search_circle_theme;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, "查找圈子");
    }

    @Override
    protected void setUpViews() {
        initSwipeLayout(swipeLayout);
        keyWords = getIntent().getStringExtra("key");
        filterEdit.setText(keyWords);
        mAdapter = new MyCircleAdapter(this);
        mAdapter.setSearch(true, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = (int) view.getTag();
                requestJoin(position);
            }
        });
        listView.setAdapter(mAdapter);

        swipeLayout.setOnRefreshListener(this);

        if (isNetvaiable()) {
            swipeLayout.post(new Thread(new Runnable() {

                @Override
                public void run() {
                    swipeLayout.setRefreshing(true);
                    onRefresh();
                }
            }));

        }
    }

    @Override
    public void onRefresh() {
        CircleAPI.searchAllCircle(currentPage, keyWords, this, null);
    }

    public void requestJoin(int position) {
        CircleModel circleModel = mAdapter.getItem(position);
        CircleAPI.addToCircleByme(circleModel.getPid(), new IAsyncHttpResponseHandler() {
            @Override
            public void start(int type) {

            }

            @Override
            public void success(int type, String json) {
                if (validateJson(json).isSuccess()) {
                    SearchCircleThemeActivity.this.finish();
                }
            }

            @Override
            public void failed(String error) {

            }

            @Override
            public void finish(int type) {

            }
        }, getLoading(R.string.watting_hint));
    }

    @Override
    public void success(int type, String json) {
        swipeLayout.setRefreshing(false);
        switch (type) {
            case CircleAPI.SEARCH_ALL_CIRCLE_CODE:
                BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                if (base.isSuccess()) {
                    JSONObject object = JSONObject.parseObject(base.data);
                    currentPage = object.getInteger(Constants.CURRENT);
                    if (currentPage == 1) {
                        mAdapter.clear();
                    }
                    listView.loadMoreFinish();
                    if (total == currentPage) {
                        listView.removeFooterView();
                    }
                    total = object.getInteger(Constants.TOTAL);

                    List<CircleModel> teamIndex = JSONObject.parseArray(JSONObject.parseObject(base.data).getString("list"),
                            CircleModel.class);
                    if (teamIndex != null && !teamIndex.isEmpty()) {
                        mAdapter.addAllAfter(teamIndex);
                    } else {
                        T.showShort(this, base.msg);
                    }
                }

                break;
        }
    }

    @Override
    public void failed(String error) {
        swipeLayout.setRefreshing(false);

    }

}
